﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DataProvider.SQL
{
    public class PipeNetworkDemoData
    {
        public static Data.DataResult QueryData(Metadata.DataSource dataSource, Metadata.DataSet dataSet, List<QueryParameter> parameters)
        {
            #region 屏幕01-01
            if (dataSet.Name.Equals("YYGDSSNL"))
            {
                return YYGDSSNL(1600, "YYGDSSNL");
            }
            else if (dataSet.Name.Equals("TRQGDSSNL"))
            {
                return YYGDSSNL(800, "TRQGDSSNL");
            }
            else if (dataSet.Name.Equals("YYTRQLNKCCL"))
            {
                return YYGDSSNL(1600, "YYTRQLNKCCL");
            }
            else if (dataSet.Name.Equals("YYTRQLNCL"))
            {
                return YYGDSSNL(6000, "YYTRQLNCL");
            }
            else if (dataSet.Name.Equals("YYJGLCPYCL"))
            {
                return YYGDSSNL(1000, "YYJGLCPYCL");
            }
            else if (dataSet.Name.Equals("CPYGSLJQTFSYSL"))
            {
                return YYGDSSNL(800, "CPYGSLJQTFSYSL");
            }
            else if (dataSet.Name.Equals("CPYLNXSL"))
            {
                return CPYLNXSL(1000, "CPYLNXSL");
            }
            else if (dataSet.Name.Equals("TRQLNXSL"))
            {
                return CPYLNXSL(700, "TRQLNXSL");
            }
            #endregion

            else if (dataSet.Name.Equals("GYTYYCLJJKL"))
            {
                return GYTYYCLJJKL(900, "GYTYYCLJJKL");
            }
            else if (dataSet.Name.Equals("DEMODATA"))
            {
                return GetDemoData(parameters);
            }
            else if (dataSet.Name.Equals("C21A5A1P25DemoData"))
            {
                return GetC21A5A1P25DemoData(parameters);
            }
            return null;
        }

        /// <summary>
        /// 原油管道输送能力
        /// </summary>
        /// <returns></returns>
        private static Data.DataResult YYGDSSNL(double leftValue, string seriesName)
        {
            Data.DataResult result = new Data.DataResult();
            result.Name = seriesName;
            result.DataSeries = new List<Data.DataSeries>();

            Random random = new Random();
            for (int i = 0; i < 2; i++)
            {
                Data.DataSeries series = new Data.DataSeries();
                series.Id = seriesName + i;
                series.Name = seriesName + i;
                series.DataItems = new List<Data.DataItem>();
                result.DataSeries.Add(series);

                for (int index = 2004; index <= 2013; index++)
                {
                    double value = random.NextDouble() * leftValue;
                    Data.DataItem item = new Data.DataItem();
                    item.Id = index.ToString();
                    item.Name = index.ToString();
                    item.Value = value.ToString("F2");
                    series.DataItems.Add(item);

                    System.Threading.Thread.Sleep(10);
                }
            }
            return result;
        }

        /// <summary>
        /// 成品油历年销售量
        /// </summary>
        /// <param name="leftValue"></param>
        /// <param name="seriesName"></param>
        /// <returns></returns>
        private static Data.DataResult CPYLNXSL(double leftValue, string seriesName)
        {
            Data.DataResult result = new Data.DataResult();
            result.Name = seriesName;
            result.DataSeries = new List<Data.DataSeries>();

            Data.DataSeries series = new Data.DataSeries();
            series.Id = seriesName;
            series.Name = seriesName;
            series.DataItems = new List<Data.DataItem>();
            result.DataSeries.Add(series);

            Random random = new Random();
            for (int index = 2004; index <= 2013; index++)
            {
                double value = random.NextDouble() * leftValue;
                Data.DataItem item = new Data.DataItem();
                item.Id = index.ToString();
                item.Name = index.ToString();
                item.Value = value.ToString("F2");
                series.DataItems.Add(item);

                System.Threading.Thread.Sleep(10);
            }

            return result;
        }

        /// <summary>
        /// 各油田原油产量及进口量
        /// </summary>
        /// <param name="leftValue"></param>
        /// <param name="seriesName"></param>
        /// <returns></returns>
        private static Data.DataResult GYTYYCLJJKL(double leftValue, string seriesName)
        {
            Data.DataResult result = new Data.DataResult();
            result.Name = seriesName;
            result.DataSeries = new List<Data.DataSeries>();

            Random random = new Random();
            for (int i = 0; i < 2; i++)
            {
                Data.DataSeries series = new Data.DataSeries();
                series.Id = seriesName;
                series.Name = seriesName;
                series.DataItems = new List<Data.DataItem>();
                result.DataSeries.Add(series);

                double value = random.NextDouble() * leftValue;
                Data.DataItem item = new Data.DataItem();
                item.Id = "DQ";
                item.Name = "大庆";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "JL";
                item.Name = "吉林";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "LH";
                item.Name = "辽河";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "HB";
                item.Name = "华北";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "DG";
                item.Name = "大港";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "JD";
                item.Name = "冀东";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "ZJ";
                item.Name = "浙江";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "XJ";
                item.Name = "新疆";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "TLM";
                item.Name = "塔里木";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "TH";
                item.Name = "吐哈";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "CQ";
                item.Name = "长庆";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "QH";
                item.Name = "青海";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "YM";
                item.Name = "玉门";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "XN";
                item.Name = "西南";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "NF";
                item.Name = "南方";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);

                value = random.NextDouble() * leftValue;
                item = new Data.DataItem();
                item.Id = "HW";
                item.Name = "海外";
                item.Value = value.ToString("F0");
                series.DataItems.Add(item);
            }
            return result;
        }

        private static Data.DataResult GetDemoData(List<QueryParameter> parameters)
        {
            Data.DataResult result = new Data.DataResult();
            result.Name = "DemoData";
            result.DataSeries = new List<Data.DataSeries>();

            string[] seriesId = parameters.Find(a => a.Name.ToUpper().Equals("SERIESID")).ValueString.Split(new char[] { ',' });
            string[] seriesName = parameters.Find(a => a.Name.ToUpper().Equals("SERIESNAME")).ValueString.Split(new char[] { ',' });
            string[] itemId = parameters.Find(a => a.Name.ToUpper().Equals("ITEMID")).ValueString.Split(new char[] { ',' });
            string[] itemName = parameters.Find(a => a.Name.ToUpper().Equals("ITEMNAME")).ValueString.Split(new char[] { ',' });
            double maxValue = double.Parse(parameters.Find(a => a.Name.ToUpper().Equals("MAXVALUE")).ValueString);
            double minValue = double.Parse(parameters.Find(a => a.Name.ToUpper().Equals("MINVALUE")).ValueString);

            Random random = new Random();
            for (int i = 0; i < seriesId.Length; i++)
            {
                Data.DataSeries series = new Data.DataSeries();
                series.Id = seriesId[i];
                series.Name = seriesName[i];
                series.DataItems = new List<Data.DataItem>();
                result.DataSeries.Add(series);

                for (int j = 0; j < itemId.Length; j++)
                {
                    double value = random.NextDouble() * (maxValue - minValue) + minValue;
                    Data.DataItem item = new Data.DataItem();
                    item.Id = itemId[j];
                    item.Name = itemName[j];
                    item.Value = value.ToString("F0");
                    series.DataItems.Add(item);
                }
            }
            return result;
        }

        /// <summary>
        /// C2.1 A5A1P2_5 勘探工作量数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private static Data.DataResult GetC21A5A1P25DemoData(List<QueryParameter> parameters)
        {
            Data.DataResult result = new Data.DataResult();
            result.Name = "DemoData";
            result.DataSeries = new List<Data.DataSeries>();

            string[] seriesId = parameters.Find(a => a.Name.ToUpper().Equals("SERIESID")).ValueString.Split(new char[] { ',' });
            string[] seriesName = parameters.Find(a => a.Name.ToUpper().Equals("SERIESNAME")).ValueString.Split(new char[] { ',' });
            string[] itemId = parameters.Find(a => a.Name.ToUpper().Equals("ITEMID")).ValueString.Split(new char[] { ',' });
            string[] itemName = parameters.Find(a => a.Name.ToUpper().Equals("ITEMNAME")).ValueString.Split(new char[] { ',' });
            double maxValue = double.Parse(parameters.Find(a => a.Name.ToUpper().Equals("MAXVALUE")).ValueString);
            double minValue = double.Parse(parameters.Find(a => a.Name.ToUpper().Equals("MINVALUE")).ValueString);

            Random random = new Random();
            for (int i = 0; i < seriesId.Length; i++)
            {
                Data.DataSeries series = new Data.DataSeries();
                series.Id = seriesId[i];
                series.Name = seriesName[i];
                series.DataItems = new List<Data.DataItem>();
                result.DataSeries.Add(series);

                for (int j = 0; j < itemId.Length; j++)
                {
                    double value = random.NextDouble() * (maxValue - minValue) + minValue;
                    Data.DataItem item = new Data.DataItem();
                    item.Id = itemId[j];
                    item.Name = itemName[j];
                    if (i <2)
                    {
                        if (j ==0)
                        {
                            item.Value = value.ToString("F0");
                        }
                        else
                        {
                            item.Value = "0";
                        }
                    }
                    else
                    {
                        if (j == 0)
                        {
                            item.Value = "0";
                        }
                        else
                        {
                            item.Value = value.ToString("F0");
                        }
                    }
                    series.DataItems.Add(item);
                }

            }
            return result;
        }
    }
}
